﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BCMS.FrontEnd.DataContract;
using BCMS.Settings;
using BCMS.Business.Implementation;

namespace BCMS.Forms.Functions
{
    public partial class frmDeleteReleasedCoupons : Form
    {
        CouponUI cp = null;
        public frmMain _parentForm { set; get; }


        public frmDeleteReleasedCoupons()
        {
            InitializeComponent();
        }

        public frmDeleteReleasedCoupons(frmMain main)
        {
            this.Font = SystemFonts.MessageBoxFont;
            InitializeComponent();
            _parentForm = main;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                DALclient client = new DALclient();
                string[] series = txtNumCode.Text.Split('-');
                //check if range is entered
                if (series.Length > 1)
                {
                    client.batchDelete(getSerialsFromUI());

                    GlobalVars.ShowInfo("Xóa [THÀNH CÔNG] " + getSerialsFromUI().Count + " phiếu.");
                }
                else
                {
                    if (cp != null)
                    {
                        if (cp.Status.Equals(Params.CLAIMED))
                        {
                            GlobalVars.ShowWarning("Coupon: [" + cp.Serial +
                                            "] [ĐÃ ĐƯỢC THU HỒI] bởi [" + cp.ClaimBy + "]");
                            ResetUI();
                            return;
                        }
                        List<string> temp = new List<string>();
                        temp.Add(getSerialFromUI());

                        client.batchDelete(temp);

                        GlobalVars.ShowInfo("Xóa [THÀNH CÔNG] coupon: [" + getSerialFromUI() + "]");
                        SortableBindingList<CouponUI> couponList = _parentForm.grvCoupons.DataSource as SortableBindingList<CouponUI>;
                        if (couponList != null)
                        {
                            _parentForm.updateReleaseList();
                        }
                        
                        ResetUI();
                        txtAlphaCode.Focus();
                    }

                }
            }
            catch (Exception ex)
            {
                GlobalVars.ShowError("Không xóa coupon\n\nError: " + ex.Message);
                return;
            }

        }

        private string getSerialFromUI()
        {
            return txtAlphaCode.Text + "-" + txtNumCode.Text;
        }


        private void getCouponInfo(string serial)
        {
            try
            {
                DALclient client = new DALclient();
                cp = client.getCouponBySerial(serial);

                if (cp != null)
                {
                    txtParValue.Value = Convert.ToInt64(cp.ParValue);
                    txtStartDate.Text = cp.StartDate.ToString("yyyy/MM/dd");
                    txtExpireDate.Text = cp.ExpireDate.ToString("yyyy/MM/dd");
                    txtStatus.Text = cp.Status;
                    if (cp.Status.Equals(Params.NORMAL))
                    {
                        btnDelete.Enabled = true;
                        btnDelete.Focus();
                    }
                    else if (cp.Status.Equals(Params.CLAIMED))
                    {
                        btnDelete.Enabled = false;
                        btnClose.Focus();
                    }
                }
                else
                {
                    GlobalVars.ShowWarning("Không tìm thấy coupon: [" +
                                    txtAlphaCode.Text + "-" + txtNumCode.Text + "]");
                    ResetUI();
                    txtAlphaCode.SelectAll();
                }
            }
            catch (Exception ex)
            {
                GlobalVars.ShowError("Lỗi thu hồi coupons: " + txtAlphaCode.Text + "-" + txtNumCode.Text +
                                "\nError: " + ex.Message);
            }

        }

        private List<string> getSerialsFromUI()
        {
            string[] series = txtNumCode.Text.Split('-');
            int clow = Convert.ToInt32(series[0]);
            int chigh = Convert.ToInt32(series[1]);
            List<string> list = new List<string>();
            for (int i = clow; i <= chigh; i++)
                list.Add(txtAlphaCode.Text + "-" + i.ToString());

            return list;
        }

        private void ResetUI()
        {
            cp = null;
            txtNumCode.Text = string.Empty;
            txtStartDate.Text = string.Empty;
            txtExpireDate.Text = string.Empty;
            txtParValue.Text = string.Empty;
            txtNote.Text = string.Empty;
            txtStatus.Text = string.Empty;
        }


    }
}
